-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathIO.vb
330 lines (307 loc) · 14.4 KB
/
IO.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
Namespace My
''' <summary>
''' 磁盘文件读写相关函数
''' </summary>
''' <remarks></remarks>
Partial Public NotInheritable Class IO
''' <summary>
''' 读取文件为Byte数组
''' </summary>
''' <param name="FilePath">文件路径(可以是相对路径)</param>
''' <returns>结果Byte数组(失败返回空Byte数组)</returns>
''' <remarks></remarks>
Public Shared Function ReadByte(ByVal FilePath As String) As Byte()
Dim Reader As System.IO.FileStream
Try
Reader = New System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
Dim Temp(Reader.Length - 1) As Byte
Reader.Read(Temp, 0, Reader.Length)
Reader.Dispose()
Return Temp
Catch ex As Exception
Return New Byte() {}
End Try
End Function
''' <summary>
''' 将Byte数组写入文件(覆盖)
''' </summary>
''' <param name="Source">Byte数组</param>
''' <param name="FilePath">文件路径(可以是相对路径)</param>
''' <returns>是否写入成功</returns>
''' <remarks></remarks>
Public Shared Function WriteByte(ByVal Source As Byte(), ByVal FilePath As String) As Boolean
Dim Writer As System.IO.FileStream
Try
Writer = New System.IO.FileStream(FilePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)
Writer.Write(Source, 0, Source.Length)
Writer.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 将Byte数组写入文件(追加)
''' </summary>
''' <param name="Source">Byte数组</param>
''' <param name="FilePath">文件路径(可以是相对路径)</param>
''' <returns>是否写入成功</returns>
''' <remarks></remarks>
Public Shared Function AppendByte(ByVal Source As Byte(), ByVal FilePath As String) As Boolean
Dim Writer As System.IO.FileStream
Try
Writer = New System.IO.FileStream(FilePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)
Writer.Write(Source, Writer.Length, Source.Length)
Writer.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 读取文件为字符串(UTF-8)
''' </summary>
''' <param name="FilePath">文件路径(可以是相对路径)</param>
''' <returns>结果字符串(失败返回空字符串)</returns>
''' <remarks></remarks>
Public Shared Function ReadString(ByVal FilePath As String) As String
Dim Reader As System.IO.StreamReader
Try
Reader = New System.IO.StreamReader(FilePath, System.Text.Encoding.UTF8)
Dim Temp As String = Reader.ReadToEnd()
Reader.Dispose()
Return Temp
Catch ex As Exception
Return ""
End Try
End Function
''' <summary>
''' 将字符串写入文件(覆盖,不包含UTF8的BOM头)
''' </summary>
''' <param name="Source">字符串</param>
''' <param name="FilePath">文件路径(可以是相对路径)</param>
''' <returns>是否写入成功</returns>
''' <remarks></remarks>
Public Shared Function WriteString(ByVal Source As String, ByVal FilePath As String) As Boolean
Dim Writer As System.IO.StreamWriter
Try
Writer = New System.IO.StreamWriter(FilePath, False, New System.Text.UTF8Encoding(False))
Writer.Write(Source)
Writer.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 将字符串写入文件(追加,不包含UTF8的BOM头)
''' </summary>
''' <param name="Source">字符串</param>
''' <param name="FilePath">文件路径(可以是相对路径)</param>
''' <returns>是否写入成功</returns>
''' <remarks></remarks>
Public Shared Function AppendString(ByVal Source As String, ByVal FilePath As String) As Boolean
Dim Writer As System.IO.StreamWriter
Try
Writer = New System.IO.StreamWriter(FilePath, True, New System.Text.UTF8Encoding(False))
Writer.Write(Source)
Writer.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 读取文件中的一维字符串数组(UTF-8,注意文件的字符串内容为空时,返回空String数组)
''' </summary>
''' <param name="FilePath">文件路径(可以是相对路径)</param>
''' <returns>结果字符串数组(失败返回空String数组)</returns>
''' <remarks></remarks>
Public Shared Function ReadStringArray(ByVal FilePath As String) As String()
If System.IO.File.Exists(FilePath) = False Then
Return New String() {}
End If
Dim FileInfo As New System.IO.FileInfo(FilePath)
If FileInfo.Length = 0 Then
Return New String() {}
End If
Dim Reader As System.IO.StreamReader
Try
Reader = New System.IO.StreamReader(FilePath, System.Text.Encoding.UTF8)
Dim Temp As String = Reader.ReadToEnd()
Reader.Dispose()
If Temp.Length = 0 Then
Return New String() {}
Else
Temp = Temp.Replace(Chr(13) + Chr(10), Chr(10))
Dim Result As String() = Temp.Split(New Char() {Chr(10)})
For I = 0 To Result.Length - 1
Result(I) = Result(I).Replace("\\", "\@")
Result(I) = Result(I).Replace("\r", Chr(13))
Result(I) = Result(I).Replace("\n", Chr(10))
Result(I) = Result(I).Replace("\@", "\")
Next
Return Result
End If
Catch ex As Exception
Return New String() {}
End Try
End Function
''' <summary>
''' 将一维字符串数组写入文件(覆盖,不包含UTF8的BOM头,注意数组的字符串内容为空时,不会实际创建或写入文件)
''' </summary>
''' <param name="StringArray">字符串数组</param>
''' <param name="FilePath">文件路径(可以是相对路径)</param>
''' <returns>是否写入成功</returns>
''' <remarks></remarks>
Public Shared Function WriteStringArray(ByVal StringArray As String(), ByVal FilePath As String) As Boolean
If StringArray.Length = 0 Then
Return False
End If
For I = 0 To StringArray.Length - 1
StringArray(I) = StringArray(I).Replace("\", "\\")
StringArray(I) = StringArray(I).Replace(Chr(13), "\r")
StringArray(I) = StringArray(I).Replace(Chr(10), "\n")
Next
Dim Builder As System.Text.StringBuilder
Dim Writer As System.IO.StreamWriter
Try
Builder = New System.Text.StringBuilder()
Builder.Append(StringArray(0))
For I = 1 To StringArray.Length - 1
Builder.Append(vbCrLf & StringArray(I))
Next
If Builder.Length = 0 Then
Return False
End If
Writer = New System.IO.StreamWriter(FilePath, False, New System.Text.UTF8Encoding(False))
Writer.Write(Builder)
Writer.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 将一维字符串数组写入文件(追加,不包含UTF8的BOM头,注意数组的字符串内容为空时,不会实际创建或写入文件)
''' </summary>
''' <param name="StringArray">字符串数组</param>
''' <param name="FilePath">文件路径(可以是相对路径)</param>
''' <returns>是否写入成功</returns>
''' <remarks></remarks>
Public Shared Function AppendStringArray(ByVal StringArray As String(), ByVal FilePath As String) As Boolean
If StringArray.Length = 0 Then
Return False
End If
For I = 0 To StringArray.Length - 1
StringArray(I) = StringArray(I).Replace("\", "\\")
StringArray(I) = StringArray(I).Replace(Chr(13), "\r")
StringArray(I) = StringArray(I).Replace(Chr(10), "\n")
Next
Dim Builder As System.Text.StringBuilder
Dim Writer As System.IO.StreamWriter
Try
Builder = New System.Text.StringBuilder()
If System.IO.File.Exists(FilePath) Then
Dim FileInfo As New System.IO.FileInfo(FilePath)
If FileInfo.Length > 0 Then
Builder.Append(vbCrLf)
End If
End If
Builder.Append(StringArray(0))
For I = 1 To StringArray.Length - 1
Builder.Append(vbCrLf & StringArray(I))
Next
If Builder.Length = 0 Then
Return False
End If
Writer = New System.IO.StreamWriter(FilePath, True, New System.Text.UTF8Encoding(False))
Writer.Write(Builder)
Writer.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 获取指定目录下的全部文件的路径列表
''' </summary>
''' <param name="SearchDirectory">要搜索的文件夹路径(默认为程序运行的当前文件夹)</param>
''' <returns>包含所有文件路径的结果字符串数组(失败返回空String数组)</returns>
''' <remarks></remarks>
Public Shared Function ListFile(Optional ByVal SearchDirectory As String = ".\") As String()
Dim FileList As List(Of String) = New List(Of String)
Dim Directory As List(Of String) = New List(Of String)
Try
For Each Temp As String In System.IO.Directory.GetFiles(SearchDirectory)
FileList.Add(Temp)
Next
For Each Temp As String In System.IO.Directory.GetDirectories(SearchDirectory)
Directory.Add(Temp)
Next
Dim Index As Integer = 0
While Directory.Count > Index
SearchDirectory = Directory(Index)
Index = Index + 1
For Each Temp As String In System.IO.Directory.GetFiles(SearchDirectory)
FileList.Add(Temp)
Next
For Each Temp As String In System.IO.Directory.GetDirectories(SearchDirectory)
Directory.Add(Temp)
Next
End While
Return FileList.ToArray()
Catch ex As Exception
Return New String() {}
End Try
End Function
''' <summary>
''' 创建快捷方式文件(覆盖)
''' </summary>
''' <param name="TargetPath">快捷方式指向的路径(可以是相对路径,如"1.exe")</param>
''' <param name="LinkFilePath">快捷方式文件的路径(可以是相对路径,如"1.lnk")</param>
''' <param name="Arguments">打开程序的参数(例如"/?")</param>
''' <param name="Description">鼠标悬停在快捷方式上的描述</param>
''' <param name="WorkingDirectory">快捷方式的起始位置(默认设置为快捷方式指向的路径的父目录)</param>
''' <returns>是否创建成功</returns>
''' <remarks></remarks>
Public Shared Function WriteLinkFile(ByVal TargetPath As String, ByVal LinkFilePath As String, Optional ByVal Arguments As String = "", Optional ByVal Description As String = "", Optional ByVal WorkingDirectory As String = "") As Boolean
Try
If System.IO.File.Exists(LinkFilePath) = True Then
System.IO.File.Delete(LinkFilePath)
End If
If TargetPath.Contains(":") = False Then
TargetPath = System.IO.Directory.GetCurrentDirectory + "\" + TargetPath
End If
If WorkingDirectory = "" Then
WorkingDirectory = System.IO.Directory.GetParent(LinkFilePath).FullName
End If
Dim Shortcut As Object = CreateObject("WScript.Shell").CreateShortcut(LinkFilePath)
Shortcut.TargetPath = TargetPath
Shortcut.IconLocation = TargetPath
Shortcut.Arguments = Arguments
Shortcut.Description = Description
Shortcut.WorkingDirectory = WorkingDirectory
Shortcut.Save()
Shortcut = Nothing
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 读取快捷方式指向的路径(获得完整的绝对路径)
''' </summary>
''' <param name="LinkFilePath">快捷方式文件的路径(可以是相对路径,如"1.lnk")</param>
''' <returns>结果字符串(失败返回空字符串"")</returns>
''' <remarks></remarks>
Public Shared Function ReadLinkFile(ByVal LinkFilePath As String) As String
Try
Dim Shortcut As Object = CreateObject("WScript.Shell").CreateShortcut(LinkFilePath)
Return Shortcut.TargetPath
Catch ex As Exception
Return ""
End Try
End Function
End Class
End Namespace